package de.mafr.jsystest.ws;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import de.mafr.jsystest.SystemTestException;


/**
 * Utilities for working with commons-httpclient classes.
 */
public class HttpUtils {
	
	private HttpUtils() {
		// to prevent instantiation
	}

	public static String getBody(HttpMethodBase method) {
		try {
			return method.getResponseBodyAsString();
		} catch (IOException e) {
			throw new SystemTestException("Fatal transport error: " + e.getMessage(), e);
		} 
	}

	public static <T> RequestEntity createRequestEntity(Request<T> request) {
		try {
			return new StringRequestEntity(request.getContent(), request.getContentType(), request.getEncoding());
		} catch (UnsupportedEncodingException e) {
			throw new SystemTestException("Invalid encoding: " + request.getEncoding(), e);
		}
	}
}
